<?php
class CF_DbTable_Table extends Zend_Db_Table_Abstract
{
	protected $_primary = 'id';
	protected $_rowset = 'CF_DbTable_Rowset';
	const POSTFIX = '_Table';
	protected $_columns = array();
	//protected $rowClass = 'CF_';
	public function init(){
		$this->rowClass = substr(get_class($this),0,-strlen(self::POSTFIX));
	}

	/**
	 *  deleteByID()
	 *  @param int $id
	 * @see Zend_Db_Table_Abstract::delete()
	 */

	public function deleteByID($id){
		$where = $this->getAdapter()->quoteInto('id = ?', $id);
		parent::delete($where);
	}
	/**
	 *
	 * getById()
	 * @param  $id
	 * @return  Zend_Db_Table_Row object
	 *
	 */
	public function getByID($id){
		$where = $this->select()
					  ->where('id=?',$id);
		return $this->fetchRow($where);
	}

	/**
	 *
	 * getAll()
	 * return defenite quantity of records
	 * depends on set columns
	 * @param unknown_type $limit
	 * @param unknown_type $start
	 * @param unknown_type $order
	 */
	public function getAll($limit = null, $start = null, array $order = null){
		$select = $this->select();
		if(!is_null($limit)){
			if(!is_null($start)){
				$select->limit($limit, $start);
			}else{
				$select->limit($limit);
			}
		}
		if(!is_null($order)){
			$select->order($order);
		}

		return $this->fetchAll($select);
	}

	/**
	 *
	 * saveByID()
	 * it contains id for updating (or special field criteria)
	 * its argument's keys consits of table columns(fields of models)
	 * and their values getting in controllers
	 * if updateData hsa not defenite key(column)
	 * $data for updating takes defoalt values
	 * if id has not been set
	 * function return false
	 * @param array $updateData
	 */
	public function saveByID(array $updateData, $id = null, $fieldName = null)
	{
		$data = array();
		if(is_null($id)){
			return false;
		}
		foreach($this->_columns as $fieldName=>$fieldDefoaltValue){

			if(!empty($updateData[$fieldName])){
				$data[$fieldName] = $updateData[$fieldName];
			}else{
				$data[$fieldName] = $fieldDefoaltValue;
			}
		}
		$fieldName = is_null($fieldName)?'id':$fieldName;
		$where = $this->getAdapter()->where($fieldName.'=?',(int)$id);

		$this->update($data, $where);
	}



	/**
	 *
	 * addRecord()
	 * its argument's keys consits of table columns(fields of models)
	 * and their values getting in controllers
	 * if updateData has not defenite key(column)
	 * $data for updating takes defoalt values
	 * if id has not been set
	 * function return false
	 * @param array $updateData
	 */
	public function addRecord(array $updateData){
		$data = array();
		foreach($this->_columns as $fieldName=>$fieldDesoaltValue){
			if(!empty($updateData[$fieldName])){
				$data[$fieldName] = $updateData[$fieldName];
			}else{
				$data[$fieldName] = $fieldDesoaltValue;
			}
		}
		$this->insert($data);
	}



/**
	 *
	 * addRecords()
	 * recycle metho addRecord
	 * @param array $updateDataRows
	 *
	 * @link http://stackoverflow.com/questions/816910/how-do-i-add-more-than-one-row-with-zend-db
	 */

	public function addRecords(array $updateDataRows){
		foreach($updateDataRows as $row){
			self::addRecord($row);
		}
	}

	/**
	 * getLastRecord
	 * order by id DESC
	 * and take 1 record
	 * contains some filters in $where(key->field and logical sign, value->value)
	 * e.g.:array('link_id='=>'10')
	 * $value needs quoting
	 * @param array $where
	 * Enter description here ...
	 */
	public function getLastRecord(array $where = null){
		$select = $this->select();
		if(!is_null($where)){
			foreach ($where as $field=>$val){
				$where = $this->getAdapter()->quoteInto($field.'?',$val);
				$select->where($where);
			}
		}
		$select->order('id DESC');
	  	$select->limit(1);
//		var_dump($resource);
//	  	exit;
	  	return $this->fetchRow($select);
	}

	/**
	 * getFirstRecord
	 * order by id DESC
	 * and take 1 record
	 * contains some filters in $where(key->field and logical sign, value->value)
	 * e.g.:array('link_id='=>'10')
	 * $value needs quoting
	 * @param array $where
	 * Enter description here ...
	 */
	public function getFirstRecord(array $where = null){
		$select = $this->select()
			 		   ->order('id');

		if(!is_null($where)){
			foreach ($where as $field=>$condition){
				$select->where($field.'?',$condition);
			}
		}
		$select->limit(1);
		return $this->fetchRow($select);
	}


}
